package com.zyx.kafka.interceptor;

import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;

/**
 * @Description: 消息拦截加时间戳
 * @Author: ZYX
 * @Date: 2022/1/13 14:05
 * @Version: 1.0
 */
public class TimestamptInterceptor implements ProducerInterceptor<String,String> {

    /**
     * 拦截器核心处理方法
     * @param producerRecord 被拦截处理的消息
     * @return
     */
    @Override
    public ProducerRecord<String, String> onSend(ProducerRecord<String, String> producerRecord) {
        // 1.获取消息对象
        String value = producerRecord.value();
        String result = System.currentTimeMillis() + "-->"+value;

        // 2.构建新的消息对象
        ProducerRecord<String, String> newRecord = new ProducerRecord<>(producerRecord.topic(), producerRecord.partition(), producerRecord.key(), result);


        return newRecord;
    }

    @Override
    public void onAcknowledgement(RecordMetadata recordMetadata, Exception e) {

    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> map) {

    }
}
